﻿using Entity = Vinno.WMS.Pack.Core.Entities.WareHouseAreaPos;
using IContractService = Vinno.WMS.Pack.Core.IWmsService;
using InputDto = Vinno.WMS.Pack.Core.Dtos.WareHouseAreaPosInputDto;
using OutputDto = Vinno.WMS.Pack.Core.Dtos.WareHouseAreaPosOutputDto;

namespace Vinno.WMS.Apis.Areas.Admin.Controllers
{
    [Description("库位信息")]
    [ModuleInfo(OrderNo = 3, Position = "Wms", PositionName = "仓储模块")]
    public class WareHouseAreaPosController : AdminControllerBase
    {
        /// <summary>
        /// 获取 服务接口
        /// </summary>
        protected IContractService ContractService => ServiceProvider.GetService<IContractService>();

        /// <summary>
        /// 读取
        /// </summary>
        [HttpPost]
        [Description("读取")]
        [ModuleInfo]
        public PageData<OutputDto> Read(PageRequest request)
        {
            Check.NotNull(request, nameof(request));
            IFunction function = this.GetExecuteFunction();

            Expression<Func<Entity, bool>> predicate = FilterService.GetExpression<Entity>(request.FilterGroup);
            var page = ContractService.WareHouseAreaPoss.ToPage(predicate,
                request.PageCondition, m => new
                {
                    D = m,
                    ParentName = $"{m.WareHouseArea.WareHouse.Name}-{m.WareHouseArea.AreaName}"
                }).ToPageResult(data => data.Select(m =>
                {
                    var outDto = m.D.MapTo<OutputDto>();
                    outDto.ParentName = m.ParentName;
                    return outDto;
                }
                ).ToArray());

            return page.ToPageData();
        }

        /// <summary>
        /// 新增
        /// </summary>
        [HttpPost]
        [DependOnFunction(nameof(Read))]
        [UnitOfWork]
        [Description("新增")]
        [ModuleInfo]
        public async Task<AjaxResult> Create(InputDto[] dtos)
        {
            Check.NotNull(dtos, nameof(dtos));
            List<string> ids = new();
            List<OutputDto> outputDtos = new();
            foreach (var dto in dtos)
            {
                var result = await ContractService.CreateWareHouseAreaPoss(dto);
                if (!result.Succeeded)
                {
                    return result.ToAjaxResult();
                }
                ids.Add(dto.Id.ToString());
                outputDtos.Add(dto.MapTo<OutputDto>());
            }
            return AjaxResult.Success(outputDtos, $"{nameof(Entity)}“{ids.ExpandAndToString()}”创建成功");
        }

        /// <summary>
        /// 更新
        /// </summary>
        [HttpPost]
        [DependOnFunction(nameof(Read))]
        [UnitOfWork]
        [Description("更新")]
        [ModuleInfo]
        public async Task<AjaxResult> Update(InputDto[] dtos)
        {
            Check.NotNull(dtos, nameof(dtos));
            List<string> ids = new();
            List<OutputDto> outputDtos = new();
            foreach (var dto in dtos)
            {
                var result = await ContractService.UpdateWareHouseAreaPoss(dto);
                if (!result.Succeeded)
                {
                    return result.ToAjaxResult();
                }
                ids.Add(dto.Id.ToString());
                outputDtos.Add(dto.MapTo<OutputDto>());
            }
            return AjaxResult.Success(outputDtos, $"{nameof(Entity)}“{ids.ExpandAndToString()}”更新成功");
        }

        /// <summary>
        /// 删除
        /// </summary>
        [HttpPost]
        [DependOnFunction(nameof(Read))]
        [UnitOfWork]
        [Description("删除")]
        [ModuleInfo]
        public async Task<AjaxResult> Delete(Guid[] ids)
        {
            Check.NotNull(ids, nameof(ids));
            var result = await ContractService.DeleteWareHouseAreaPoss(ids);
            return result.ToAjaxResult();
        }


        /// <summary>
        /// 导出
        /// </summary>
        [HttpPost]
        [Description("导出")]
        [ModuleInfo]
        public AjaxResult Export(FilterGroup group)
        {
            Check.NotNull(group, nameof(group));
            Expression<Func<Entity, bool>> predicate = FilterService.GetExpression<Entity>(group);
            var data = ContractService.WareHouseAreaPoss.Where(predicate).MapToList<Entity, OutputDto>();
            return AjaxResult.Success(data);
        }

    }
}